package TestProject01.uti;

import java.util.ArrayList;
import java.util.Random;

public class CodeUtil {

    public static String getCode() {
        //1、创建一个集合
        ArrayList<Character> list = new ArrayList<>();//52  索引的范围：0~51
        //2、添加字母 a-z  A-Z
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }

        //3、打印集合
        System.out.println(list);

        //4、生成4个随机字母
        String result = "";
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int randomIndex = r.nextInt(list.size());
            char c = list.get(randomIndex);
            result = result + c;
        }

        //5、生成1个随机数字
        int number = r.nextInt(10);
        result = result + number;

        //6、打乱顺序
        char[] arr = result.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            int index = r.nextInt(arr.length);
            char temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }

        //7、将字符数组转换为字符串
        String newResult = new String(arr);
        return newResult;
    }
}
